package inftel.android;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;



import dk.itu.android.bluetooth.BluetoothAdapter;
import dk.itu.android.bluetooth.BluetoothDevice;
import dk.itu.android.bluetooth.BluetoothServerSocket;
import dk.itu.android.bluetooth.BluetoothSocket;



import android.app.Activity;
/*
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BluetoothDiscoverActivity extends Activity 
{
	static final int REQUEST_BLUETOOTH_ENABLE = 1;
		
	BluetoothAdapter btAdapter;
	ArrayAdapter<String> discoveredDevicesArrayAdapter;	
		
	BroadcastReceiver discoveryReceiver = new BroadcastReceiver()
    {
		@Override
		public void onReceive(Context context, Intent intent)
		{
			if(intent.getAction().contentEquals(BluetoothDevice.ACTION_FOUND))
			{
				BluetoothDevice discoveredDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				discoveredDevicesArrayAdapter.add("Remote_BtDevice_MAC\n"+discoveredDevice.getAddress());
			}
		}
    };
    
    OnItemClickListener deviceClickListener = new OnItemClickListener()
    {
        @Override
        public void onItemClick(android.widget.AdapterView<?> av, View v, int arg2, long arg3) 
        {
            btAdapter.cancelDiscovery();
            
            String text = ((TextView)v).getText().toString();
            String mac = text.split("\n")[1];

            Intent btIntent = new Intent(BluetoothDiscoverActivity. this,BluetoothConnectionActivity.class);
            btIntent.putExtra("MAC", mac);
			startActivity(btIntent);
			unregisterReceiver(discoveryReceiver);
            finish();
        }
    };


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bt_main);
        BluetoothAdapter.SetContext(getBaseContext()); 
        
        discoveredDevicesArrayAdapter = new ArrayAdapter<String>(this,R.layout.device_address);
        ListView discoveredDevicesListView = (ListView)findViewById(R.id.DiscoveredDevicesListView);
        discoveredDevicesListView.setAdapter(discoveredDevicesArrayAdapter);
        discoveredDevicesListView.setOnItemClickListener(deviceClickListener);
               
    }
    
    @Override
    protected void onStart() 
    {
    	// TODO Auto-generated method stub
    	super.onStart();
        
		setUpDeviceBluetoothAdapter();
    }
    
    
    
   
   
    
    public void setUpDeviceBluetoothAdapter() 
    {
    	 btAdapter = BluetoothAdapter.getDefaultAdapter();
 	     registerReceiver(discoveryReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
        
         if (!btAdapter.isEnabled()) 
         {	
             Toast.makeText(this, "Bluetooth is disabled. Enabling Bluetooth module...", Toast.LENGTH_LONG).show();	
             startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_BLUETOOTH_ENABLE);
             Toast.makeText(this, "... Bluetooth is enabled now", Toast.LENGTH_LONG).show();	
             Toast.makeText(this,"Local_BtDevice_MAC\n"+btAdapter.getAddress(), Toast.LENGTH_LONG).show();	
         }
         else
         {
             Toast.makeText(this,"Error trying to enable Bluetooth", Toast.LENGTH_LONG).show();		
         } 
       
     	 btAdapter.startDiscovery(); //TODO with startActivityForResult
     	 
    }   

}



